<?php

class P2X2P_Client extends P2X2P {
	
	public $serverURL = null;
	public $username = null;
	public $password = null;
	public $timeout = false;
	
	public $faultCode = false;
	public $faultString = false;
	
	public function __construct($serverURL=null, $username=null, $password=null, $timeout=false) {
		parent::__construct();
		$this->serverURL = $serverURL;
		$this->username = $username;
		$this->password = $password;
		$this->timeout = $timeout;
	}
	
	public function call_Function() {
		// ====================
		// = !! Deprecated !! =
		// ====================
		Log::append("Deprecated", "$file:$line - P2X2P_Client:call_Function()");
		call_user_method_array('call', $this, func_get_args());
	}
	
	public function call($methodName) {
		
		// =================
		// = Pack RPC Call =
		// =================
		
		$methodCallNode = $this->DOM->createElement('methodCall');
		
		$methodNameNode = $this->DOM->createElement('methodName');
		$methodNameNode->appendChild($this->DOM->createTextNode($methodName));
		
		$parametersNode = $this->DOM->createElement('params');
		
		foreach(array_slice(func_get_args(), 1) as $arg) {
			$parameterNode = $this->DOM->createElement('param');
			$parameterNode->appendChild($this->encode($arg));
			$parametersNode->appendChild($parameterNode);
		}
		
		$methodCallNode->appendChild($methodNameNode);
		$methodCallNode->appendChild($parametersNode);
		
		$this->DOM->appendChild($methodCallNode);
		
		// =================
		// = Send RPC Call =
		// =================
		
		$requestXML = $this->DOM->saveXML();
		
		#header('content-type: text/xml'); echo $requestXML; exit;
		
		$requestHeaders = array("Content-type: text/xml","Content-length: " . strlen($requestXML));

		$ch = curl_init($this->serverURL);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		
		if($this->timeout) {
			curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
		}
		
		curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);

		if($this->username != null) {
			curl_setopt($ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}");
		}
		
		$responseXML = curl_exec($ch);
		
		if(curl_errno($ch)) {
			Error::handler(null, 'P2X2P/CURL Error #' . curl_errno($ch) . ' - ' . curl_error($ch), __FILE__, __LINE__);
		}
		
		curl_close($ch);
		
		// =========================
		// = Validate RPC Response =
		// =========================
		
		if(empty($responseXML)) {
			Error::handler(null, 'P2X2P Error - Empty Response', __FILE__, __LINE__);
		}
		
		#header('content-type:text/xml');echo $responseXML; exit;
		
		if(!$this->DOM->loadXML($responseXML)) {
			Error::handler(null, 'P2X2P Error - Unable to parse response xml', __FILE__, __LINE__);
		}
			
		if(!$this->DOM->schemaValidate(Plex::$BASE_DIR . 'driver/P2X2P/xmlrpc.xsd')) {
			Error::handler(null, 'P2X2P Error - Response did not pass Schema', __FILE__, __LINE__);
		}
		
		$methodNode = $this->DOM->getElementsByTagName('methodResponse')->item(0);
		
		// ===================
		// = Check For Fault =
		// ===================
		
		if($methodNode->getElementsByTagName('fault')->length>=1)
		{
			$faultNode = $methodNode->getElementsByTagName('fault')->item(0)->firstChild;
			$faultArray = $this->decode($fault_value_node);
			
			if($this->debug) {
				Error::handler(null, "P2X2P Fault #{$faultArray['faultCode']} - {$faultArray['faultString']}", __FILE__, __LINE__);
			} else {
				$this->faultCode=$faultArray['faultCode'];
				$this->faultString=$faultArray['faultString'];
				return false;
			}			
		}
		
		// ==================
		// = Return Results =
		// ==================
		
		$parameters = array();
		$parameterNodes = $methodNode->getElementsByTagName('params')->item(0)->getElementsByTagName('param');
		
		for ($i = 0; $i < $parameterNodes->length; $i++)
		$parameters[] = $this->decode($parameterNodes->item($i)->firstChild);
		
		if(count($parameters==1)) {
			return $parameters[0];
		} else {
			return $parameters;
		}
	}
}
